// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: CPZ Setup Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_CPZSETUP

// Function declarations
reserve function CPZSetup_Function97
reserve function CPZSetup_Function98
reserve function CPZSetup_Function99
reserve function CPZSetup_Function100
reserve function CPZSetup_Function101
reserve function CPZSetup_Function102

// Static Values
public value ZoneSetup_value33 = 0;
public value ZoneSetup_value34 = 0;
public value ZoneSetup_value35 = 0;
public value ZoneSetup_value36 = 0;

// Tables
public table CPZSetup_table39
	0xE00000, 0xC00000, 0xA00000, 0x800000, 0x600000, 0x400000, 0x200000, 0x400000, 0x600000, 0x800000, 0xA00000, 0xC00000, 0xE00000, 0xE02000, 0xE04000, 0xE02000
end table

public table CPZSetup_table40
	0xE000, 0x20C000, 0x40A000, 0x608000, 0x806000, 0xA04000, 0xC02000, 0xE00000, 0xC00020, 0xA00040, 0x800060, 0x600080, 0x4000A0, 0x2000C0, 224, 0x20C0
	0x40A0, 0x6080, 0x8060, 0xA040, 0xC020
end table

public table CPZSetup_table41
	0x40E0, 192, 192, 0x60E0, 0x20C0, 192, 0x40E0, 0x40E0, 192, 0x20C0, 0x60E0, 0x20C0, 192, 0x40E0, 0x40C0, 192
	0x20C0, 0x40E0, 192, 192, 0x60E0, 0x20C0, 192, 0x40E0, 0x20E0, 192, 0x20C0
end table

public table CPZSetup_table38
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
end table

//0xF8CB
private table CPZSetup_table0
	0xE000, 0xA000, 0x6000, 0x2000, 0, 32, 96, 0x20A0, 0x60E0, 0x20A0, 96, 32, 0, 0x200000, 0x600000, 0xA00000, 0xE00000, 0xE04000, 0xE00000, 0xA00000, 0x600000, 0x200000, 0, 0x202000, 0x606000, 0xA0A000, 0xE0E000, 0xA0A000, 0x606000, 0x202000, 0, 0x2000, 0x6000, 0xA000
end table

private table CPZSetup_table1
	0x380000, 0x1EC0000, 0, 10, 8, 52, 56, 1, 40, 5, 8, 35, 56, 1, 40, 8, 8, 255, 8, 214, 0, 9, 4, 3, 52, 1, 36, 9, 40, 2, 8, 54, 0, 2, 4, 2, 0, 1, 8, 192, 56, 1, 40, 41, 8, 13, 10, 1, 2, 26, 10, 2, 8, 16, 0, 2, 4, 22, 0, 8, 8, 34, 56, 1, 40, 3, 8, 32, 0, 1, 4, 10, 0, 1, 8, 10, 0, 16, 4, 39, 0, 1, 2, 20, 0, 1, 4, 29, 0, 37, 4, 12, 0, 4, 4, 4, 0, 24, 48, 1, 32, 4, 4, 7, 0, 20, 4, 30, 0
end table

private table CPZSetup_table2
	0x380000, 0x1F00000, 0, 53, 8, 45, 56, 1, 40, 6, 8, 34, 56, 1, 40, 9, 8, 55, 56, 1, 40, 21, 8, 249, 56, 1, 40, 12, 8, 84, 56, 1, 40, 21, 8, 138, 56, 1, 40, 9, 42, 16, 10, 9, 8, 96, 10, 4, 2, 9, 10, 2, 8, 71, 0, 11, 4, 44, 0, 13, 4, 4, 0, 4, 4, 10, 0, 9, 4, 9, 0, 3, 4, 39, 0, 6, 4, 11, 0, 7, 4, 5, 0, 13, 48, 1, 32, 3, 40, 1, 8, 5, 56, 1, 40, 3, 32, 2, 0, 4, 48, 1, 32, 4, 0, 4, 56, 1, 40, 4, 8, 4, 56, 1, 32, 5, 0, 3, 48, 1, 32, 6, 0, 3, 4, 1, 52, 1, 36, 5, 4, 3, 52, 1, 36, 1, 32, 4, 0, 4, 56, 1, 40, 4, 8, 2, 0, 3, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 56, 1, 40, 4, 8, 4, 56, 1, 40, 1, 32, 4, 0, 4, 48, 1, 32, 4, 0, 4, 48, 1, 32, 5, 0, 4, 48, 1, 32, 5, 0, 3, 8, 12, 0, 11, 8, 34, 56, 1, 40, 21, 8, 3, 0, 13, 8, 119, 56, 2
end table

private table CPZSetup_table3
	0x380000, 0x1EC0000, 0, 54, 8, 45, 56, 1, 40, 7, 8, 34, 56, 1, 40, 20, 8, 255, 8, 218, 56, 1, 40, 9, 8, 61, 56, 1, 40, 9, 8, 14, 0, 1, 4, 14, 0, 6, 8, 110, 0, 10, 8, 10, 0, 24, 8, 5, 0, 7, 8, 20, 56, 1, 40, 1, 32, 3, 0, 5, 4, 39, 0, 26, 8, 57, 56, 1, 40, 16, 8, 7, 56, 1, 40, 22, 41, 2, 9, 31, 8, 21, 56, 1, 40, 11, 8, 175, 10, 1, 2, 244, 50, 1, 34, 26, 42, 1, 40, 1, 8, 4, 56, 1, 40, 24, 8, 2, 0, 25, 8, 5, 56, 1, 40, 10, 8, 32, 0, 1, 4, 23, 0, 4, 8, 20, 
end table

function CPZSetup_Function97
	object.value6 = temp1
	object.value6 >>= 16
	object.value7 = temp1
	object.value7 >>= 8
	object.value7 &= 255
	object.value8 = temp1
	object.value8 &= 255
	object.value9 = temp2
	object.value9 >>= 16
	object.value10 = temp2
	object.value10 >>= 8
	object.value10 &= 255
	object.value11 = temp2
	object.value11 &= 255
	if object.value9 == 0
		object.value9 = 1
	end if
	if object.value10 == 0
		object.value10 = 1
	end if
	if object.value11 == 0
		object.value11 = 1
	end if
	object.value6 <<= 8
	object.value7 <<= 8
	object.value8 <<= 8
	object.value6 /= object.value9
	object.value7 /= object.value10
	object.value8 /= object.value11
	if object.value6 == 0
		object.value6 = 256
	end if
	if object.value7 == 0
		object.value7 = 256
	end if
	if object.value8 == 0
		object.value8 = 256
	end if
end function


function CPZSetup_Function98
	temp0 = 0
	GetPaletteEntry(1, 2, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 3, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 4, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	GetPaletteEntry(1, 5, temp1)
	SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
	temp0++
	CopyPalette(1, 0, 4, 2, 4)
	while temp0 < 64
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 0, temp1)
		temp0++
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 1, temp1)
		temp0++
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 2, temp1)
		temp0++
		GetTableValue(temp1, temp0, PlayerObject_SonicSuperPal)
		SetPaletteEntry(3, 3, temp1)
		temp0 -= 3
		SetPaletteFade(5, 3, 4, 48, 0, 4)
		GetPaletteEntry(5, 0, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(5, 1, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(5, 2, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
		GetPaletteEntry(5, 3, temp1)
		SetTableValue(temp1, temp0, PlayerObject_SonicSuperAltPal)
		temp0++
	loop
end function


function CPZSetup_Function99
	temp0 = 0
	GetPaletteEntry(1, 13, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 17, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 18, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	GetPaletteEntry(1, 16, temp1)
	SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
	temp0++
	while temp0 < 24
		GetPaletteEntry(0, 13, temp1)
		GetPaletteEntry(1, 13, temp2)
		CallFunction(CPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 248
		temp1 /= object.value6
		temp2 *= 248
		temp2 /= object.value7
		temp3 *= 248
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 17, temp1)
		GetPaletteEntry(1, 17, temp2)
		CallFunction(CPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 18, temp1)
		GetPaletteEntry(1, 18, temp2)
		CallFunction(CPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
		GetPaletteEntry(0, 16, temp1)
		GetPaletteEntry(1, 16, temp2)
		CallFunction(CPZSetup_Function97)
		GetTableValue(temp1, temp0, PlayerObject_TailsSuperPal)
		temp2 = temp1
		temp2 >>= 8
		temp2 &= 255
		temp3 = temp1
		temp3 &= 255
		temp1 >>= 16
		temp1 *= 256
		temp1 /= object.value6
		temp2 *= 256
		temp2 /= object.value7
		temp3 *= 256
		temp3 /= object.value8
		temp1 &= 240
		temp2 &= 240
		temp3 &= 240
		if temp1 > 224
			temp1 = 224
		end if
		if temp2 > 224
			temp2 = 224
		end if
		if temp3 > 224
			temp3 = 224
		end if
		temp1 <<= 16
		temp2 <<= 8
		temp1 += temp2
		temp1 += temp3
		SetTableValue(temp1, temp0, PlayerObject_TailsSuperAltPal)
		temp0++
	loop
end function


function CPZSetup_Function100
	temp0 = 0
	GetPaletteEntry(1, 26, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
	GetPaletteEntry(1, 27, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
	GetPaletteEntry(1, 28, temp1)
	SetTableValue(temp1, temp0, PlayerObject_KnuxSuperAltPal)
	temp0++
end function


function CPZSetup_Function101
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("ChemicalPlant_F.ogg", 0, 0x85AFA, 0x1F40)
			break
		case 2
			SetMusicTrack("ChemicalPlant_F.ogg", 0, 0x85AFA)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("ChemicalPlant_F.ogg", 0, 0x85AFA)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function CPZSetup_Function102
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
                        SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("ChemicalPlantAct2.ogg", 0, 0xA7224, 0x30D4)
			break
		case 2
			SetMusicTrack("ChemicalPlantAct2.ogg", 0, 0xA7224)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("ChemicalPlantAct2.ogg", 0, 0xA7224)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function CPZSetup_Function103
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("ChemicalPlantAct2_F.ogg", 0, 0x7477E, 0x1F40)
			break
		case 2
			SetMusicTrack("ChemicalPlantAct2_F.ogg", 0, 0x7477E)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("ChemicalPlantAct2_F.ogg", 0, 0x7477E)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function CPZSetup_Function104
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
                        SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("ChemicalPlantAct2.ogg", 0, 0x919CB, 0x30D4)
			break
		case 2
			SetMusicTrack("ChemicalPlantAct2.ogg", 0, 0x919CB)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("ChemicalPlantAct2.ogg", 0, 0x919CB)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function



event ObjectMain
	GetTableValue(temp0, 21, StageSetup_table9)
	if temp0 == 0
		ZoneSetup_value33++
		ZoneSetup_value33 &= 3
	end if
	object.value13++
	if object.value13 > 7
		tileLayer[1].deformationOffset++
		object.value13 = 0
	end if
	object.value0++
	if object.value0 == 5
		object.value0 = 0
		object.frame++
		object.frame &= 7
		temp0 = object.frame
		temp0 += 768
		Copy16x16Tile(767, temp0)
	end if
	if ZoneSetup_value34 > 0
		ZoneSetup_value34--
	end if
	object.value14++
	if object.value14 == 8
		object.value14 = 0
		object.value15++
		object.value15 &= 15
		GetTableValue(temp0, object.value15, CPZSetup_table39)
		SetPaletteEntry(0, 175, temp0)
		object.value16++
		object.value16 %= 21
		GetTableValue(temp0, object.value16, CPZSetup_table40)
		SetPaletteEntry(0, 191, temp0)
		object.value17 += 3
		object.value17 %= 27
		GetTableValue(temp0, object.value17, CPZSetup_table41)
		SetPaletteEntry(0, 188, temp0)
		object.value18 += 3
		object.value18 %= 27
		GetTableValue(temp0, object.value18, CPZSetup_table41)
		SetPaletteEntry(0, 189, temp0)
		object.value19 += 3
		object.value19 %= 27
		GetTableValue(temp0, object.value19, CPZSetup_table41)
		SetPaletteEntry(0, 190, temp0)
	end if
	if ZoneSetup_value35 == 0
		if stage.debugMode == 0
			if stage.actNum == 2
				if object[30].type == TypeName[Act Finish]
					ZoneSetup_value35 = 1
					if ZoneSetup_value36 == 0
						CallNativeFunction2(SetAchievement, 1, 100)
					end if
				end if
			end if
		end if
	end if
end event


event ObjectDraw
	if object.value12 == 0
		temp0 = stage.waterLevel
		temp0 -= screen.yoffset
		if temp0 < 0
			temp0 = 0
		end if
		if temp0 > screen.ysize
			temp0 = screen.ysize
		end if
		SetActivePalette(0, 0, temp0)
		if ZoneSetup_value34 > 0
			SetActivePalette(2, temp0, screen.ysize)
		else
			SetActivePalette(1, temp0, screen.ysize)
		end if
		AddDrawListEntityRef(2, object.entityPos)
	else
		temp0 = screen.xoffset
		temp0 *= 6
		temp0 >>= 3
		temp0 &= 511
		FlipSign(temp0)
		temp0 += object.value2
		temp1 = screen.yoffset
		temp1 >>= 1
		temp1 &= 255
		FlipSign(temp1)
		DrawSpriteScreenXY(0, temp0, temp1)
		DrawSpriteScreenXY(1, temp0, temp1)
	end if
	object.value12 ^= 1
end event


event ObjectStartup
	LoadSpriteSheet("CPZ/Objects.gif")
	SetPaletteEntry(0, 192, 0)
	
	if stage.actNum == 1

		SetMusicTrack("ChemicalPlantAct2.ogg", 0, 0xA7224)
		SpeedUpMusic = CPZSetup_Function101
		SlowDownMusic = CPZSetup_Function102
	end if
	if stage.actNum == 2
		SetMusicTrack("ChemicalPlantAct2.ogg", 0, 0x919CB)
		SpeedUpMusic = CPZSetup_Function103
		SlowDownMusic = CPZSetup_Function104
	end if

	
	SpriteFrame(-16, 0, 32, 256, 224, 0)
	SpriteFrame(-16, 256, 32, 256, 224, 0)
	Copy16x16Tile(767, 768)
	animalType1 = TypeName[Locky]
	animalType2 = TypeName[Pocky]
	object[10].type = TypeName[CPZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].drawOrder = 0
	object[10].value2 = 496
	object[10].value18 = 1
	object[10].value19 = 2
	temp0 = screen.xcenter
	temp0 >>= 2
	object[10].value2 -= temp0
	ZoneSetup_value33 = 0
	LoadPalette("CPZ_WaterPal.act", 1, 0, 0, 256)
	CallFunction(CPZSetup_Function98)
	CallFunction(CPZSetup_Function99)
	CallFunction(CPZSetup_Function100)
	LoadPalette("ElectricFlash.act", 2, 0, 0, 256)
	ZoneSetup_value34 = 0
	arrayPos0 = 0
	while arrayPos0 < 576
		temp0 = arrayPos0
		temp0 &= 63
		GetTableValue(stage[arrayPos0].deformationData2, temp0, CPZSetup_table38)
		arrayPos0++
	loop
	if options.attractMode == 1
		switch stage.playerListPos
		case 0
			PlayerObject_ReplayTable = CPZSetup_table1
			PlayerObject_ReplaySize = 109
			PlayerObject_ReplayLength = 0x708
			break
		case 1
			PlayerObject_ReplayTable = CPZSetup_table2
			PlayerObject_ReplaySize = 216
			PlayerObject_ReplayLength = 0x618
			break
		case 2
			PlayerObject_ReplayTable = CPZSetup_table3
			PlayerObject_ReplaySize = 126
			PlayerObject_ReplayLength = 0x690
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
